package com.yanqu.road.dao.impl.activity.xingyoushanshui;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiEventInfo;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiRoleInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class XingYouShanShuiConfigDaoImpl extends TempDao {

    private static String getXingYouShanShuiEventInfoSql = "select * from `t_s_activity_shanshui_event` where `activityId`=?";
    public List<XingYouShanShuiEventInfo> getXingYouShanShuiEventInfoList(int activityId) {
        List<XingYouShanShuiEventInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXingYouShanShuiEventInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XingYouShanShuiEventInfo data = new XingYouShanShuiEventInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setWeight(rs.getInt("weight"));
                data.setParam(StringUtils.stringToIntegerList(rs.getString("param"), "\\|"));
                data.setEmotion(rs.getInt("emotion"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getXingYouShanShuiGridInfoSql = "select * from `t_s_activity_shanshui_grid` where `activityId`=?";
    public List<XingYouShanShuiGridInfo> getXingYouShanShuiGridInfoList(int activityId) {
        List<XingYouShanShuiGridInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXingYouShanShuiGridInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XingYouShanShuiGridInfo gridInfo = new XingYouShanShuiGridInfo();
                gridInfo.setActivityId(rs.getInt("activityId"));
                gridInfo.setId(rs.getInt("id"));
                gridInfo.setName(rs.getString("name"));
                gridInfo.setDesc(rs.getString("desc"));
                gridInfo.setType(rs.getInt("type"));
                gridInfo.setSort(rs.getInt("sort"));
                gridInfo.setRewardShow(rs.getString("rewardShow"));
                gridInfo.setTriggerType(rs.getInt("triggerType"));
                gridInfo.setParam(StringUtils.stringToIntegerList(rs.getString("param"), "\\|"));
                gridInfo.setReward1(rs.getString("reward1"));
                gridInfo.setReward2(rs.getString("reward2"));
                gridInfo.setReward3(rs.getString("reward3"));
                gridInfo.setEmotion(rs.getString("emotion"));
                if(rs.getMetaData().getColumnCount() > 12 ){
                    gridInfo.setQuality(rs.getInt("quality"));
                }
                list.add(gridInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getXingYouShanShuiRoleInfoSql = "select * from `t_s_activity_shanshui_role` where `activityId`=?";
    public List<XingYouShanShuiRoleInfo> getXingYouShanShuiRoleInfoList(int activityId) {
        List<XingYouShanShuiRoleInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXingYouShanShuiRoleInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XingYouShanShuiRoleInfo data = new XingYouShanShuiRoleInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setSkillName(rs.getString("skillName"));
                data.setSkillDesc(rs.getString("skillDesc"));
                data.setSkillParam(StringUtils.stringToIntegerList(rs.getString("skillParam"), "\\|"));
                data.setDice(StringUtils.stringToIntegerList(rs.getString("dice"), "\\|"));
                data.setRoleDesc(rs.getString("roleDesc"));
                data.setPrice(rs.getString("price"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
